<?php
/**
 * Notes:
 * User: wudg <544038230@qq.com>
 * Date: 2025/3/31 14:16
 */

namespace App\Components\Logs;


use App\Constants\System\VisLogConst;
use App\Utils\IpAddressUtils;
use App\Utils\RequestHeaderUtil;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class LogFactory
{
    /**
     * 格式化登录日志格式
     * @param int $user_id
     * @param String $account
     * @param String $success
     * @param string $message
     * @return void
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function saveLoginLog(int $user_id,String $account,String $success = "Y",string $message="登录系统"): array
    {
        list($br,$os) = RequestHeaderUtil::getBrowserAndOs();

        $ip = RequestHeaderUtil::getClientIp();
        return [
            'user_id' => $user_id,
            'name' => $account,
            'success' => $success,
            'message' => $message,
            'ip' => $ip,
            'location' => (new IpAddressUtils($ip))->getProvinceAndCity(),
            'browser' => $br,
            'os' => $os,
            'vis_type' => VisLogConst::VIS_LOGIN,

        ];
    }


    /**
     * 退出系统日志
     * @param int $user_id
     * @param String $account
     * @param String $success
     * @param string $message
     * @return array
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function saveLogoutLog(int $user_id,String $account,String $success = "Y",string $message="登出系统"): array
    {
        list($br,$os) = RequestHeaderUtil::getBrowserAndOs();
        $ip = RequestHeaderUtil::getClientIp();
        return [
            'user_id' => $user_id,
            'name' => $account,
            'success' => $success,
            'message' => $message,
            'ip' => $ip,
            'location' => (new IpAddressUtils($ip))->getProvinceAndCity(),
            'browser' => $br,
            'os' => $os,
            'vis_type' => VisLogConst::VIS_LOGOUT,

        ];
    }


    /**
     * 消费记录写入
     * @param int $uid
     * @param int $accountId
     * @param int $cate_id
     * @param int $amount
     * @param int $type
     * @param string $cardNo
     * @param array $relation
     * @param string $remark
     * @param string $ip
     * @return array
     */
    public static function saveUserAmountLogs(int $uid,int $accountId,int $cate_id,int $amount,int $type,string $cardNo = '',array $relation = [],string $remark = '',string $ip = '')
    {
        return [
            'uid' => $uid,
            'cate_id' => $cate_id,
            'account_id' => $accountId,
            'amount' => $amount,
            'card_no' => $cardNo,
            'remark' => $remark,
            'relation_info' => json_encode($relation, JSON_UNESCAPED_UNICODE),
            'ip' => $ip,
            'type' => $type,

        ];
    }
}